tree-changes
Compare changes between two datasets.
The React hook introduced in v0.6 now has its own package: tree-changes-hook.
Setup
npm install tree-changes
Usage
import treeChanges from 'tree-changes';
const previousData = {
hasData: false,
sort: {
data: [{ type: 'asc' }, { type: 'desc' }],
status: 'idle',
},
};
const newData = {
hasData: true,
sort: {
data: [{ type: 'desc' }, { type: 'asc' }],
status: 'success',
},
};
const { changed, changedFrom } = treeChanges(previousData, newData);
changed();
changed('hasData');
changed('hasData', true);
changed('hasData', true, false);
changed('sort.data.0.type', 'desc');
changed('sort.status', ['done', 'success']);
changedFrom('sort.status', 'idle');
Works with arrays too.
import treeChanges from 'tree-changes';
const { changed } = treeChanges([0, { id: 2 }], [0, { id: 4 }]);
changed();
changed(0);
changed(1);
changed('1.id', 4);
It uses @gilbarbara/deep-equal to compare properties.
API
added(key: Key
, value?: Value
)
Check if something was added to the data.
Works with arrays and objects (using Object.keys).
import treeChanges from 'tree-changes';
const previousData = {
actions: {},
messages: [],
};
const newData = {
actions: { complete: true },
messages: ['New Message'],
sudo: true,
};
const { added } = treeChanges(previousData, newData);
added();
added('actions');
added('messages');
added('sudo');
changed(key?: Key
, actual?: Value
, previous?: Value
)
Check if the data has changed.
It also can compare to the actual
value or even with the previous
.
changedFrom(key: Key
, previous: Value
, actual?: Value
)
Check if the data has changed from previous
or from previous
to actual
.
decreased(key: Key
, actual?: Value
, previous?: Value
)
Check if both values are numbers and the value has decreased.
It also can compare to the actual
value or even with the previous
.
import treeChanges from 'tree-changes';
const previousData = {
ratio: 0.9,
retries: 0,
};
const newData = {
ratio: 0.5,
retries: 1,
};
const { decreased } = treeChanges(previousData, newData);
decreased('ratio');
decreased('retries');
emptied(key: Key
)
Check if the data was emptied. Works with arrays, objects and strings.
import treeChanges from 'tree-changes';
const previousData = {
data: { a: 1 },
items: [{ name: 'test' }],
missing: 'username',
};
const newData = {
data: {},
items: [],
missing: '',
};
const { emptied } = treeChanges(previousData, newData);
emptied('data');
emptied('items');
emptied('missing');
filled(key: Key
)
Check if the data was filled (from a previous empty value). Works with arrays, objects and strings.
import treeChanges from 'tree-changes';
const previousData = {
actions: {},
messages: [],
username: '',
};
const newData = {
actions: { complete: true },
messages: ['New Message'],
username: 'John',
};
const { filled } = treeChanges(previousData, newData);
filled('actions');
filled('messages');
filled('username');
increased(key: Key
, actual?: Value
, previous?: Value
)
Check if both values are numbers and the value has increased.
It also can compare to the actual
value or even with the previous
.
import treeChanges from 'tree-changes';
const previousData = {
ratio: 0.9,
retries: 0,
};
const newData = {
ratio: 0.5,
retries: 1,
};
const { increased } = treeChanges(previousData, newData);
increased('retries');
increased('ratio');
removed(key: Key
, value?: Value
)
Check if something was removed from the data.
Works with arrays and objects (using Object.keys).
import treeChanges from 'tree-changes';
const previousData = {
data: { a: 1 },
items: [{ name: 'test' }],
switch: false,
};
const newData = {
data: {},
items: [],
};
const { removed } = treeChanges(previousData, newData);
removed();
removed('data');
removed('items');
removed('switch');
Types
type Key = string | number;
type ValidTypes = string | boolean | number | Record<string, any> };
type Value = ValidTypes | ValidTypes[];
License
MIT